// page/user/user.js
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');

Page({
    data: {
		options: null,
		trips: [],
		userInfo: null,
		windowWidth: 0,
		windowHeight: 0
    },
    onLoad: function(options) {
		let self = this;
		let id = options.id;
		self.setData({
			options
		})
		wx.getSystemInfo({
			success: function(res) {
				let windowWidth = res.windowWidth;
				let windowHeight = res.windowHeight;
				self.setData({
					windowWidth,
					windowHeight
				})
			},
		})
		api.user.userInfo(id,(state,res) => {
			if(state === 'success'){
				// console.log(res.data);
				let trips = res.data.trips;
				trips.map((trip) => {
					let item = trip;
					trip.date_added = util.formatTime(new Date(trip.date_added*1000),1)
					return item;
				})
				let userInfo = res.data.user_info;
				self.setData({
					trips,
					userInfo
				})
			}
		})
    },
	viewTrip: function(e){
		let dataTrip = e.currentTarget.dataset;
		wx.navigateTo({
			url: `../trip/trip?id=${dataTrip.id}&name=${dataTrip.name}`,
		})
	}
})